package com.lideru.backstage.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lideru.backstage.entity.BookLabel;
import com.lideru.backstage.mapper.BookLabelMapper;
import com.lideru.backstage.utils.common.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;


@RestController
@RequestMapping("/bookLabel")

public class BookLabelController {
    @Resource
    BookLabelMapper bookLabelMapper;

    @PostMapping
    public Result<?> save(@RequestBody BookLabel BookLabel ) {
        int i = bookLabelMapper.insert(BookLabel);
        if (i<1){
            return Result.error("-1","新增书本标签失败！");
        }
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody BookLabel BookLabel) {
        int i = bookLabelMapper.updateById(BookLabel);
        if (i<1){
            return Result.error("-1","修改书本标签失败！");
        }
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id ) {
        BookLabel bookLabel = bookLabelMapper.selectOne(Wrappers.<BookLabel>lambdaQuery()
                .eq(BookLabel::getId, id));
        if (bookLabel != null) {
            bookLabelMapper.deleteById(id);
            return Result.success();
        }
        return Result.error("403", "你没有权限");

    }

    @GetMapping("getAllBookLabel")
    public Result<?> getAllBookLabel(){
      return Result.success( bookLabelMapper.selectList(null));
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search
                              ) {
        return Result.success(
                bookLabelMapper.selectPage(new Page<>(pageNum, pageSize), Wrappers.<BookLabel>lambdaQuery().like(BookLabel::getLabel, search)));
    }


}
